﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Models;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
    /// <summary>
    /// 颜色管理
    /// </summary>
    public class ColorDAL
    {
        //查询所有
        public DataTable ColorListAll(string color) {
            string sql = string.Empty;
            if (!string.IsNullOrWhiteSpace(color)) {
                sql += " and mcolor like '%" + color + "%'";
            }
            return SqlHelper.ExecuteDataTable("select * from menxingcolor where isdel =0 "+sql +" order by edittime desc");
        }
        //根据id查询
        public MenxingColor GetMenxingColorById(string id) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from menxingColor where mid=@mid and isdel=0",new MySqlParameter("@mid",id));
            if (dt.Rows.Count == 1)
            {
                return ToMenxingColor(dt.Rows[0]);

            }
            else {
                throw new Exception("颜色查询有多个或没有，请联系管理员");
            }
        }
        //转换成model
        public MenxingColor ToMenxingColor(DataRow dr) {
            MenxingColor mc = new MenxingColor();
            mc.Mid = Convert.ToInt32(dr["mid"]);
            mc.Mcolor = dr["mcolor"].ToString();
            mc.Mprice = dr["mprice"].ToString();
            mc.AddTime = dr["addTime"].ToString();
            mc.EditTime = dr["edittime"].ToString();
            mc.AddUser = dr["edituser"].ToString();
            mc.EditUser = dr["adduser"].ToString();
            mc.Gylb = dr["Gylb"].ToString();
            mc.Isold = dr["Isold"].ToString();
            mc.IsDel = Convert.ToInt32(dr["IsDel"]);
            return mc;
        }
        //保存数据
        public int SaveData(MenxingColor mc) {
            return SqlHelper.ExecuteNonQuery(@"insert into menxingcolor(mcolor,mprice,isold,gylb,addtime,edittime,adduser,edituser,isdel) 
                   values(@mcolor,@mprice,@isold,@gylb,@addtime,@edittime,@adduser,@edituser,@isdel)",
                   new MySqlParameter("@mcolor", mc.Mcolor), new MySqlParameter("@mprice", mc.Mprice),new MySqlParameter("@isold", mc.Isold),
                   new MySqlParameter("@gylb", mc.Gylb), new MySqlParameter("@addtime", DateTime.Now.ToString()), new MySqlParameter("@edittime", DateTime.Now.ToString()),
                   new MySqlParameter("@adduser",RemeberName.Username ),new MySqlParameter("@edituser",RemeberName.Username ),new MySqlParameter("@isdel","0"));
        }
        //更新数据
        public int UpdateData(MenxingColor mc) {
            return SqlHelper.ExecuteNonQuery(@"update menxingcolor set mcolor=@mcolor,mprice =@mprice,isold=@isold,gylb=@gylb,edittime=@edittime,edituser=@edituser where mid=@mid",
                new MySqlParameter("@mcolor", mc.Mcolor), new MySqlParameter("@mprice", mc.Mprice),new MySqlParameter("@isold", mc.Isold),new MySqlParameter("@gylb", mc.Gylb), 
                new MySqlParameter("edittime", DateTime.Now.ToString()),new MySqlParameter("edituser",RemeberName.Username), new MySqlParameter("@mid",mc.Mid));
        }

        //删除数据
        public int DelDataById(string id) {
            return SqlHelper.ExecuteNonQuery("update menxingcolor set isdel=1 where mid = @mid",new MySqlParameter("@mid",id));
        }


    }
}
